package cn.tedu.csmall.stock.webapi.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {
    //创建一个JobDetail，封装Job，并交给Spring管理
    @Bean
    public JobDetail showTime(){
        System.out.println("showTime方法运行了");
        return JobBuilder.newJob(QuartzJob.class)
                .withIdentity("datetime") //给JobDetail起个名字
                .storeDurably() //即使没有触发器绑定当前JobDetail，他也不会被删除
                .build();
    }

    //下面要声明触发器对象，它设置job何时运行，也需要保存到Spring容器中
    @Bean
    public Trigger showTimeTrigger(){
        System.out.println("showTimeTrigger方法运行");
        //定义Cron表示
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        return TriggerBuilder.newTrigger()
                //绑定到要运行的JobDetail对象中
                .forJob(showTime())
                .withIdentity("dateTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail addStock(){
        return JobBuilder.newJob(StockJob.class)
                .withIdentity("addStock")
                .storeDurably()
                .build();
    }
    @Bean
    public Trigger addStockTrigger(){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(addStock())
                .withIdentity("addTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
